const app = getApp(); //引入导航

var util = require('../../utils/common.js'); //引入公共JS


import { YcSearch } from 'ycSearch.model.js';
var ycsearch = new YcSearch();
Page({
  data: {
    showIcon: true,
    //显示返回键以及首页
    cityData: "全国",
    cityFlag: true,
    //城市
    SearchClose: true,
    SearchTo: false,
    classification_yc: false,
    classification_jq: true,
    history: ['轻松', '树叶', 'a', 'a', 'a', 'a', 'a', 'a'],
    _typeid: '',
    jqyc: '演出'
  },
  onLoad: function (options) {
    console.log(options);
    var source = options.source;

    if (source) {
      tt.setStorageSync('source', source);
    }

    var cityData = options._city;

    if (cityData !== undefined) {
      this.data._typeid = options._typeid;

      if (this.data._typeid == 0) {
        this.data._typeid = '';
      }

      this.setData({
        cityData: cityData
      });
    } //判断是否为景区搜索进入


    var jqyc = options.jqyc;

    if (jqyc != undefined) {
      this.data.jqyc = jqyc;
    }

    if (this.data.jqyc == '景区') {
      this.setData({
        classification_jq: false,
        classification_yc: true
      });
    }

    var inputdata = options.inputdata;
    this.setData({
      inputdata: inputdata
    });

    this._onLoad(inputdata);
  },
  //关闭页面时隐藏城市弹框
  onHide: function () {
    this.setData({
      cityFlag: true,
      //关闭城市
      searchFlag: true,
      //关闭搜索列表
      inputdata: "",
      //清空输入框的值
      SearchClose: true,
      SearchTo: false
    });
  },
  _onLoad: function (inputdata) {
    console.log(inputdata); // this.data.inputdata = inputdata;

    var patt3 = /^[\u4e00-\u9fa5]{2,20}$/; //正则验证汉字不能少两个，不能多余20个

    if (inputdata != '') {
      //搜索页面
      var Strategy = ycsearch.getSearchFor1(inputdata, res => {
        var res2 = [];

        if (this.data._typeid !== '') {
          console.log(res.length);

          for (var a = 0; a < res.length; a++) {
            if (res[a]['city'] == this.data._typeid) {
              res2.push(res[a]);
            }
          }

          res = res2;
        }

        this.setData({
          'SearchFor_yc': res,
          searchFlag: false
        });
      }); //搜索页面

      ycsearch.getSearchFor(inputdata, res => {
        var res2 = [];

        if (this.data._typeid !== '') {
          console.log(res.length);

          for (var a = 0; a < res.length; a++) {
            if (res[a]['city'] == this.data._typeid) {
              res2.push(res[a]);
            }
          }

          res = res2;
        }

        this.setData({
          'SearchFor_jq': res,
          searchFlag: false
        });
      });
    } else {
      this.setData({
        searchFlag: true
      });
    }

    ; //查询省列表

    ycsearch.getShengHuiRow(res => {
      this.setData({
        'ShengHui': res
      });
    });
  },
  //打开搜索
  searchValue: function (e) {
    var _data = e.detail.value;
    var patt3 = /^[\u4e00-\u9fa5]{2,20}$/; //正则验证汉字不能少两个，不能多余20个

    this.setData({
      //保存输入框的值
      inputdata: e.detail.value
    });
    this.data.datarow = _data;

    if (_data == '') {
      this.setData({
        SearchClose: true,
        SearchTo: false,
        inputdata: ""
      }); // } else if(patt3.test(_data)){
    } else if (_data != '') {
      console.log(_data); //搜索页面

      var Strategy = ycsearch.getSearchFor1(this.data.datarow, res => {
        var res2 = [];

        if (this.data._typeid !== '') {
          console.log(res.length);

          for (var a = 0; a < res.length; a++) {
            if (res[a]['city'] == this.data._typeid) {
              res2.push(res[a]);
            }
          }

          res = res2;
        }

        var yc_length = res.length;
        this.setData({
          'SearchFor_yc': res,
          SearchClose: false,
          SearchTo: true
        });
      }); //搜索页面

      ycsearch.getSearchFor(this.data.datarow, res => {
        var res2 = [];

        if (this.data._typeid !== '') {
          console.log(res.length);

          for (var a = 0; a < res.length; a++) {
            if (res[a]['city'] == this.data._typeid) {
              res2.push(res[a]);
            }
          }

          res = res2;
        }

        var jq_length = res.length;
        this.setData({
          'SearchFor_jq': res,
          searchFlag: false
        });
      });
    }
  },
  //点击关闭按钮关闭搜索框
  toClose: function () {
    this.setData({
      SearchClose: true,
      SearchTo: false,
      inputdata: "" //清空输入框的值

    });
  },
  classification_yc: function () {
    this.setData({
      classification_yc: false,
      classification_jq: true
    });
  },
  classification_jq: function () {
    this.setData({
      classification_jq: false,
      classification_yc: true
    });
  },
  //打开关闭城市
  onCloseCity: function (e) {
    this.setData({
      cityFlag: true
    });
  },
  // 拨打电话
  calling: function () {
    util.calling();
  },
  //分享
  onShareAppMessage: function () {
    util.sharePage();
  },
  //回到顶部
  goTop: function () {
    // 一键回到顶部
    util.goTop();
  }
});